package mysql

import (
	"video-up-manager/videorpc/videorpc"

	"gorm.io/gorm"
)

type Comment struct {
	gorm.Model
	VideoId        int64  `gorm:"type:int(11);not null;comment:'视频ID'"`
	UserId         int64  `gorm:"type:int(11);not null;comment:'用户ID'"`
	CommentContent string `gorm:"type:varchar(500);not null;comment:'评论内容'"`
	IsAnonymous    int64  `gorm:"type:tinyint;default:0;comment:'是否匿名'"`
	CommentTime    string `gorm:"type:varchar(50);not ull;comment:'评论时间'"`
	CommentStatus  string `gorm:"type:enum('1','0');default:'0';comment:'评论状态'"`
	ReplyId        int64  `gorm:"type:int(11);comment:'回复ID'"`
}

func (c *Comment) CommentAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Comment) CommentList(db *gorm.DB, in *videorpc.CommentListRequest, lists []*videorpc.CommentList) ([]*videorpc.CommentList, error) {
	/*
		SELECT
		  comments.id as comment_id,videos.video_title,users.nick_name,comments.comment_content,comments.is_anonymous,comments.comment_time,comments.comment_status,comments.reply_id
		FROM
		  comments
		  LEFT JOIN videos ON videos.id= comments.video_id
		  LEFT JOIN users ON users.id = comments.user_id
		  ORDER BY comments.comment_time DESC
	*/
	err := db.Debug().Model(c).
		Select("comments.id as comment_id,videos.video_title,users.nick_name,comments.comment_content,comments.is_anonymous,comments.comment_time,comments.comment_status,comments.reply_id").
		Joins("LEFT JOIN videos ON videos.id= comments.video_id").
		Joins("LEFT JOIN users ON users.id = comments.user_id").
		Order("comments.comment_time desc").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Find(&lists).Error
	return lists, err
}

func (c *Comment) CommentReply(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Comment) CommentDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("comments.id=?", id).Delete(c).Error
}
